import SwiftUI
import SPGlassKit

public struct GlassSidebarExample: GlassExample {
    public static let componentID = "glass-sidebar"
    @State private var selection: String? = "inbox"
    @State private var columnVisibility: NavigationSplitViewVisibility = .automatic

    public init() {}

    private let sections: [GlassSidebarSection<String>] = [
        GlassSidebarSection(id: "mailboxes", title: "Mailboxes", items: [
            GlassSidebarItem(id: "inbox", title: "Inbox", systemImage: "tray", badge: 12, keywords: ["unread"]),
            GlassSidebarItem(id: "drafts", title: "Drafts", systemImage: "doc", badge: 2),
            GlassSidebarItem(id: "sent", title: "Sent", systemImage: "paperplane"),
            GlassSidebarItem(id: "archive", title: "Archive", systemImage: "archivebox"),
        ]),
        GlassSidebarSection(id: "labels", title: "Labels", items: [
            GlassSidebarItem(id: "travel", title: "Voyages et séjours", systemImage: "airplane", keywords: ["travel", "trips"]),
            GlassSidebarItem(id: "receipts", title: "Reçus", systemImage: "receipt", keywords: ["receipts"]),
            GlassSidebarItem(id: "long", title: "A label with a deliberately long name to check wrapping", systemImage: "tag"),
        ]),
    ]

    public var body: some View {
        GlassSidebar("Mail", sections: sections, selection: $selection, columnVisibility: $columnVisibility, searchPrompt: "Search mailboxes") { id in
            if let id {
                VStack(spacing: 12) {
                    Text(verbatim: title(of: id)).font(.title2)
                    Button(columnVisibility == .detailOnly ? "Show sidebar" : "Hide sidebar") {
                        columnVisibility = columnVisibility == .detailOnly ? .all : .detailOnly
                    }
                }
                .padding()
                .navigationTitle(Text(verbatim: title(of: id)))
            } else {
                ContentUnavailableView("Select a mailbox", systemImage: "sidebar.left")
            }
        }
    }

    private func title(of id: String) -> String {
        sections.flatMap(\.items).first { $0.id == id }?.title ?? id
    }
}

#Preview("GlassSidebar") { GlassSidebarExample() }
